"""
WTForms definitions for the Flask Forms demo.
"""
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed, FileRequired
from wtforms import StringField, PasswordField, TextAreaField, IntegerField, SelectField, BooleanField, SubmitField
from wtforms.validators import DataRequired, Length, Email, NumberRange, EqualTo, ValidationError
import re


class RegistrationForm(FlaskForm):
    """Registration form with various field types and validation."""
    
    username = StringField('Username', validators=[
        DataRequired(),
        Length(min=3, max=20, message='Username must be between 3 and 20 characters')
    ])
    
    email = StringField('Email', validators=[
        DataRequired(),
        Email(message='Please enter a valid email address')
    ])
    
    password = PasswordField('Password', validators=[
        DataRequired(),
        Length(min=8, message='Password must be at least 8 characters long')
    ])
    
    confirm_password = PasswordField('Confirm Password', validators=[
        DataRequired(),
        EqualTo('password', message='Passwords must match')
    ])
    
    age = IntegerField('Age', validators=[
        DataRequired(),
        NumberRange(min=13, max=120, message='Age must be between 13 and 120')
    ])
    
    country = SelectField('Country', choices=[
        ('', 'Select your country'),
        ('us', 'United States'),
        ('uk', 'United Kingdom'),
        ('ca', 'Canada'),
        ('au', 'Australia'),
        ('de', 'Germany'),
        ('fr', 'France'),
        ('jp', 'Japan'),
        ('cn', 'China'),
        ('in', 'India'),
        ('other', 'Other')
    ], validators=[DataRequired()])
    
    biography = TextAreaField('Biography', validators=[
        Length(max=500, message='Biography must be less than 500 characters')
    ])
    
    agree_terms = BooleanField('I agree to the Terms and Conditions', validators=[
        DataRequired(message='You must agree to the terms and conditions')
    ])
    
    subscribe_newsletter = BooleanField('Subscribe to newsletter')
    
    submit = SubmitField('Register')
    
    def validate_username(self, username):
        """Custom username validation."""
        # Check for allowed characters (letters, numbers, underscores only)
        if not re.match("^[A-Za-z0-9_]+$", username.data):
            raise ValidationError('Username can only contain letters, numbers, and underscores')
        
        # Check for reserved usernames
        reserved_names = ['admin', 'root', 'user', 'test']
        if username.data.lower() in reserved_names:
            raise ValidationError('This username is reserved')
    
    def validate_email(self, email):
        """Custom email validation."""
        # Check for disallowed domains
        disallowed_domains = ['spam.com', 'fake.com']
        domain = email.data.split('@')[-1].lower()
        if domain in disallowed_domains:
            raise ValidationError('Emails from this domain are not allowed')


class AvatarUploadForm(FlaskForm):
    """Form for avatar upload with file validation."""
    
    avatar = FileField('Avatar', validators=[
        FileAllowed(['jpg', 'jpeg', 'png', 'gif'], 'Only image files are allowed'),
        FileRequired('Please select an image file')
    ])
    
    submit = SubmitField('Upload Avatar')


class DocumentUploadForm(FlaskForm):
    """Form for document upload with file validation."""
    
    documents = FileField('Documents', validators=[
        FileAllowed(['pdf', 'doc', 'docx', 'txt'], 'Only document files are allowed')
    ])
    
    submit = SubmitField('Upload Documents')